package com.sunlgggg.view.component;

import com.google.gson.Gson;
import com.sunlgggg.object.StudentInfo;

import javax.swing.*;
import java.awt.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

import static com.sunlgggg.util.HttpRequestUtils.post;
import static javax.swing.JOptionPane.*;


public class MangerPanel extends JPanel {
    JLabel title;
    JPanel titlePanel;
    StudentInfoTable studentInfoTable;

    public MangerPanel() {

        this.setLayout(new BorderLayout());
        titlePanel = new JPanel();
        titlePanel.setPreferredSize(new Dimension(600, 50));
        title = new JLabel("学生信息", SwingConstants.CENTER);
        titlePanel.setLayout(new BorderLayout());
        titlePanel.add(title, BorderLayout.CENTER);
        studentInfoTable = new StudentInfoTable();

        JScrollPane jScrollPane_IL = new JScrollPane(studentInfoTable);
//        jScrollPane_IL.setBounds(5, 5, 637, 275);
        this.add(titlePanel, BorderLayout.NORTH);
        this.add(jScrollPane_IL, BorderLayout.CENTER);
        ManagerOperation managerOperation = new ManagerOperation();
        this.add(managerOperation, BorderLayout.SOUTH);


        studentInfoTable.load();

        studentInfoTable.getSelectionModel().addListSelectionListener(e -> {
            int row = studentInfoTable.getSelectedRow();
            if (row != -1) {
                managerOperation.stuIdJTextField.setText((String) studentInfoTable.getValueAt(row, 0));
                managerOperation.nameJTextField.setText((String) studentInfoTable.getValueAt(row, 1));
                managerOperation.genderJTextField.setText((String) studentInfoTable.getValueAt(row, 2));
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy   MM  dd");
                Date date = null;
                try {
                    date = simpleDateFormat.parse(studentInfoTable.getValueAt(row, 3).toString());
                } catch (ParseException e1) {
                    e1.printStackTrace();
                }
                managerOperation.bornDateJTextField.setDate(date == null ? new Date() : date);
                managerOperation.chineseJTextField.setText((String) studentInfoTable.getValueAt(row, 4));
                managerOperation.mathJTextField.setText((String) studentInfoTable.getValueAt(row, 5));
                managerOperation.englishJTextField.setText((String) studentInfoTable.getValueAt(row, 6));
            }
        });

        //添加学生
        managerOperation.addStudentButton.addActionListener(e -> {
            if (managerOperation.stuIdJTextField.getText().trim().equals("")
                    || managerOperation.genderJTextField.getText().trim().equals("")
                    || managerOperation.nameJTextField.getText().trim().equals("")
                    || managerOperation.bornDateJTextField.getDate() == null
                    || managerOperation.chineseJTextField.getText().trim().equals("")
                    || managerOperation.mathJTextField.getText().trim().equals("")
                    || managerOperation.englishJTextField.getText().trim().equals("")) {
                showConfirmDialog(this, "请补全学生信息", "提示", CLOSED_OPTION, INFORMATION_MESSAGE);
                return;
            }
            //checked the studentInfo ...

            if (!"男".equals(managerOperation.genderJTextField.getText()) && !"女".equals(managerOperation.genderJTextField.getText())) {
                showConfirmDialog(this, "性别为男或者女", "提示", CLOSED_OPTION, INFORMATION_MESSAGE);
                return;
            }

            if (Double.valueOf(managerOperation.mathJTextField.getText()) > 100 || Double.valueOf(managerOperation.mathJTextField.getText()) < 0) {
                showConfirmDialog(this, "数学成绩应大于等于0，小于等于100之间", "提示", CLOSED_OPTION, INFORMATION_MESSAGE);
                return;
            }

            if (Double.valueOf(managerOperation.chineseJTextField.getText()) > 100 || Double.valueOf(managerOperation.mathJTextField.getText()) < 0) {
                showConfirmDialog(this, "语文成绩应大于等于0，小于等于100之间", "提示", CLOSED_OPTION, INFORMATION_MESSAGE);
                return;
            }
            if (Double.valueOf(managerOperation.englishJTextField.getText()) > 100 || Double.valueOf(managerOperation.mathJTextField.getText()) < 0) {
                showConfirmDialog(this, "英语成绩应大于等于0，小于等于100之间", "提示", CLOSED_OPTION, INFORMATION_MESSAGE);
                return;
            }


            StudentInfo studentInfo = new StudentInfo();
            studentInfo.setStuId(managerOperation.stuIdJTextField.getText());
            studentInfo.setGender(managerOperation.genderJTextField.getText());
            studentInfo.setName(managerOperation.nameJTextField.getText());
            studentInfo.setBornDate(managerOperation.bornDateJTextField.getDate());
            studentInfo.setChineseScore(Double.valueOf(managerOperation.chineseJTextField.getText()));
            studentInfo.setMathScore(Double.valueOf(managerOperation.mathJTextField.getText()));
            studentInfo.setEnglishScore(Double.valueOf(managerOperation.englishJTextField.getText()));
            String ret = post("info/addInfo", "studentinfo=" + new Gson().toJson(studentInfo));
            if (ret.equals("existed")) {
                showConfirmDialog(this, "用户已经存在", "提示", CLOSED_OPTION, INFORMATION_MESSAGE);
            } else if (ret.equals("success")) {
                showConfirmDialog(this, "添加成功", "提示", CLOSED_OPTION, INFORMATION_MESSAGE);
                studentInfoTable.load();
            } else {
                showConfirmDialog(this, "添加失败", "提示", CLOSED_OPTION, INFORMATION_MESSAGE);
            }

        });

        managerOperation.modifyStudentButton.addActionListener(e -> {
            StudentInfo studentInfo = new StudentInfo();
            studentInfo.setStuId(managerOperation.stuIdJTextField.getText());
            studentInfo.setGender(managerOperation.genderJTextField.getText());
            studentInfo.setName(managerOperation.nameJTextField.getText());
            studentInfo.setBornDate(new Date());
            studentInfo.setChineseScore(Double.valueOf(managerOperation.chineseJTextField.getText()));
            studentInfo.setMathScore(Double.valueOf(managerOperation.mathJTextField.getText()));
            studentInfo.setEnglishScore(Double.valueOf(managerOperation.englishJTextField.getText()));
            String ret = post("info/modify", "studentinfo=" + new Gson().toJson(studentInfo));
            if (ret.equals("success")) {
                showConfirmDialog(this, "修改成功", "提示", CLOSED_OPTION, INFORMATION_MESSAGE);
                studentInfoTable.load();
            } else if (ret.equals("nouser")) {
                showConfirmDialog(this, "用户不存在", "提示", CLOSED_OPTION, INFORMATION_MESSAGE);
            } else {
                showConfirmDialog(this, "修改失败", "提示", CLOSED_OPTION, INFORMATION_MESSAGE);
            }
        });

        managerOperation.deleteStudentButton.addActionListener(e -> {
            int n = showConfirmDialog(null, "确认删除吗?", "确认对话框", YES_NO_OPTION);
            if (n == YES_OPTION) {
                if (post("info/delete", "stuId=" + managerOperation.stuIdJTextField.getText()).equals("success")) {
                    showMessageDialog(new JFrame(), "删除成功");
                    studentInfoTable.load();
                } else {
                    showMessageDialog(new JFrame(), "不存在该用户");
                }
            }
        });
    }
}

